package com.community.service;

import com.community.bean.Role;
import com.community.bean.User;
import com.feng.fengDUtil.BaseService;
import com.community.util.Constants;
import com.community.util.Search;
import com.community.util.TreeUtil;
import com.feng.fengDUtil.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MenuService extends BaseService {

    private static final Logger log = LoggerFactory.getLogger(MenuService.class);

    public List<Map> allMenus() {
        List<Map> allMenus = sql("select m.code id,m.pcode pid,m.text,m.url,m.power access from menu m order by m.code").list(Map.class);

        return TreeUtil.geneTree(allMenus);
    }



    public List<Map> allMenus(Integer roleId) {
        List<Map> allMenus = sql("select m.code id,m.pcode pid,m.text,m.url,m.power,r.access from menu m left join role_menu r on m.code = r.menuId and r.roleID = ? order by m.code").list(Map.class,roleId);
        return TreeUtil.geneTree(allMenus);
    }

    public List<Map> allRoles() {
        List<Map> allRoles = selectAll("role");
        return allRoles;
    }


    public List<Map> getUserMenu(User user) {
        if(Constants.ADMIN_USER_ID.equals(user.getId())){
           return allMenus();
        }else if(user.getRoleId()!=null){
            String sql = "select m.code id,m.pcode pid,m.text,m.url,r.access from menu m join role_menu r on m.code = r.menuId where r.roleId = ? order by m.code";
            List tarList = sql(sql).list(Map.class,user.getRoleId());
            List allMenus = sql("select m.code id,m.pcode pid,m.text,m.url,m.power from menu m order by m.code asc").list(Map.class);
            TreeUtil.addParents(tarList,allMenus);
            return TreeUtil.geneTree(tarList);
            // mysql 8 语法
           /* String sql="with recursive sons as (SELECT m.code ,m.pcode  FROM menu m join role_menu r on r.menuId = m.code where  r.roleId = ? union" +
                    "                        SELECT p.code ,p.pcode  FROM menu p,sons where p.code = sons.pcode  ) " +
                    "     select code id,pcode pid,text,url,(select r.access from role_menu r where r.menuId = m.code  ) access from menu m where m.code in (select code from sons) order by code";
            return TreeUtil.geneTree(sql(sql).list(Map.class,user.getRoleId()));*/
        }else{
            return Collections.emptyList();
        }
    }

    public Page listRole(Page page, Search search) {
        ArrayList params = new ArrayList();
        String sql = "select u.* " +
                " from role u where 1=1 ";
        if(StringUtils.isNotEmpty(search.getName())){
            sql+=" and u.name like ? ";
            params.add(processLike(search.getName()));
        }
        sql+=" order by id asc";
        findPageList(Map.class,sql,page,params.toArray());
        return page;
    }

    public void save(Role role) {
        boolean isInsert = role.getId() == null;
        saveOrUpdate(role);
        List<String> menuIds = new ArrayList();
        role.getMenus().forEach(menu -> {menuIds.add(menu.getMenuId());menu.setRoleId(role.getId());});
        if(!isInsert){
            ArrayList params = new ArrayList();
            params.add(role.getId());
            params.addAll(menuIds);
            sql("delete r from role_menu r where r.roleId = ? and r.menuId not in "+genePlaceHolders(menuIds.size())).update(params.toArray());
        }
        saveOrUpdateAll(role.getMenus());
    }


}
